package org.jeecg.modules.lab.sample.record.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.lab.entrust.entity.LabEntrustItems;
import org.jeecg.modules.lab.entrust.entity.LabEntrustSample;
import org.jeecg.modules.lab.sample.entity.LabSampleHandleList;
import org.jeecg.modules.lab.sample.entity.LabSampleRecord;
import org.jeecg.modules.lab.sample.record.mapper.LabSampleRecordMapper;
import org.jeecg.modules.lab.sample.record.service.ILabSampleRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Description: 样品流转记录表
 * @Author: jeecg-boot
 * @Date: 2023-08-08
 * @Version: V1.0
 */
@Service
public class LabSampleRecordServiceImpl extends ServiceImpl<LabSampleRecordMapper, LabSampleRecord> implements ILabSampleRecordService {

    @Autowired
    private LabSampleRecordMapper labSampleRecordMapper;

    /**
     * 样品入库 写入样品流程记录
     *
     * @param labEntrustSampleList
     * @param mark
     * @param userName
     */
    @Override
    public void addRecords(List<LabEntrustSample> labEntrustSampleList, String mark, String userName) {
        for (LabEntrustSample entrustSample : labEntrustSampleList) {
            LabSampleRecord labSampleRecord = new LabSampleRecord();
            labSampleRecord.setSamType(entrustSample.getSamType());
            labSampleRecord.setSamId(entrustSample.getId());
            labSampleRecord.setSamName(entrustSample.getSamName());
            labSampleRecord.setSamNum(entrustSample.getSamNum());
            labSampleRecord.setRecordMark(mark);
            String quality = entrustSample.getSamQuality();
            if (quality != null && !"".equals(quality)) {
                quality = quality.replace("g", "").replace("k", "").replace("m", "")
                        .replace("G", "").replace("K", "").replace("M", "").replace("L", "")
                        .replace("(", "").replace(")", "").replace("/", "");
                if (quality.matches("-?\\d+(\\.\\d+)?")) {
                    labSampleRecord.setSamQuantity(Double.valueOf(quality));
                } else {
                    labSampleRecord.setSamQuantity(0.00);
                }
            } else {
                labSampleRecord.setSamQuantity(0.00);
            }
            labSampleRecord.setReceivePerson(userName);
            labSampleRecord.setOperatePerson(userName);
            labSampleRecord.setRecordTime(new DateTime());
            labSampleRecord.setBusId(entrustSample.getEntrustId());
            labSampleRecord.setSynFlag("0");
            labSampleRecord.setRecordRemark(labSampleRecord.getRecordTime() + "" + userName + "进行样品登记入库。");
            labSampleRecordMapper.insert(labSampleRecord);
        }
    }


    /**
     * 实验过程中 写入样品流转记录
     *
     * @param busId
     * @param entrustItems
     */
    @Override
    public void addLabRecords(String busId, LabEntrustItems entrustItems) {

        String samId = entrustItems.getSamId();
        String userName = entrustItems.getUserName();
        QueryWrapper<LabSampleRecord> queryWrapper = new QueryWrapper();
        queryWrapper.eq("bus_id", busId);
        queryWrapper.eq("sam_id", samId);
        queryWrapper.orderByAsc("create_time");
        List<LabSampleRecord> labSampleRecords = labSampleRecordMapper.selectList(queryWrapper);
        if (labSampleRecords != null && labSampleRecords.size() > 0) {
            LabSampleRecord labSampleRecord = labSampleRecords.get(labSampleRecords.size() - 1);
            String receivePerson = labSampleRecord.getReceivePerson();
            String sendPerson = labSampleRecord.getSendPerson();
            //todo 更新流转记录  再写实验记录
            if ("".equals(receivePerson) &&  !userName.equals(sendPerson)) {
                //更新流转记录
                labSampleRecord.setSynFlag("1");
                labSampleRecord.setReceivePerson(userName);
                labSampleRecordMapper.updateById(labSampleRecord);
            }
            //再写入实验记录
            LabSampleRecord record = new LabSampleRecord();
            record.setItemId(entrustItems.getItemId());
            record.setItemName(entrustItems.getItemName());
            record.setSamType(labSampleRecord.getSamType());
            record.setSamId(labSampleRecord.getSamId());
            record.setSamName(labSampleRecord.getSamName());
            record.setSamNum(labSampleRecord.getSamNum());
            record.setRecordMark("实验");
            record.setOperatePerson(userName);
            record.setSendPerson(userName);
            record.setRecordTime(new DateTime());
            record.setBusId(busId);
            record.setSynFlag("0");
            String samUsed = entrustItems.getSamUsed();
            if (samUsed != null && !"".equals(samUsed)) {
                record.setSamQuantity(Double.valueOf(entrustItems.getSamUsed()));
                record.setRecordRemark(record.getRecordTime() + "" + userName + "进行实验消耗样品" + entrustItems.getSamUsed() + "。");
            } else {
                record.setSamQuantity(0.00);
                record.setRecordRemark(record.getRecordTime() + "" + userName + "进行实验消耗样品0.00。");
            }
            labSampleRecordMapper.insert(record);
        }
    }

    /**
     * 样品回收 写入样品流转记录
     *
     * @param labEntrustSample
     */
    @Override
    public void addBackRecords(LabEntrustSample labEntrustSample, LoginUser loginUser) {

        String userName = loginUser.getRealname();
        QueryWrapper<LabSampleRecord> queryWrapper = new QueryWrapper();
        queryWrapper.eq("sam_id", labEntrustSample.getId());
        queryWrapper.orderByAsc("create_time");
        List<LabSampleRecord> labSampleRecords = labSampleRecordMapper.selectList(queryWrapper);
        LabSampleRecord record = new LabSampleRecord();
        // todo 样品分为主剂和辅剂 辅剂可能没有流转记录
        if (labSampleRecords != null && labSampleRecords.size() > 0) {
            //有流转记录的样品回收【通常是主剂】
            LabSampleRecord labSampleRecord = labSampleRecords.get(labSampleRecords.size() - 1);
            String receivePerson = labSampleRecord.getReceivePerson();
            record.setSendPerson(receivePerson);
            record.setRecordRemark(record.getRecordTime() + "" + receivePerson + "将样品退回仓库中。");
        } else {
            //没有流转记录的样品回收【通常是辅剂】
            record.setRecordRemark(record.getRecordTime() + "由" + userName + "将样品回收进仓库中。");
        }
        record.setSamType(labEntrustSample.getSamType());
        record.setSamId(labEntrustSample.getId());
        record.setSamName(labEntrustSample.getSamName());
        record.setSamNum(labEntrustSample.getSamNum());
        record.setRecordMark("回收");
        record.setReceivePerson(userName);
        record.setOperatePerson(userName);
        record.setRecordTime(new DateTime());
        record.setBusId(labEntrustSample.getEntrustId());
        record.setSynFlag("0");
        labSampleRecordMapper.insert(record);
    }


    /**
     * 样品处置 写入样品流转记录
     *
     * @param labSampleHandle
     * @param loginUser
     */
    @Override
    public void addHandRecords(LabSampleHandleList labSampleHandle, LoginUser loginUser) {

        String userName = loginUser.getRealname();
        LabSampleRecord record = new LabSampleRecord();
        record.setSamType(labSampleHandle.getSamType());
        record.setSamId(labSampleHandle.getSamId());
        record.setSamName(labSampleHandle.getSamName());
        record.setSamNum(labSampleHandle.getSamNum());
        record.setRecordMark("处置");
        record.setOperatePerson(userName);
        record.setRecordTime(new DateTime());
        record.setBusId(labSampleHandle.getHandleId());
        record.setSynFlag("0");
        record.setRecordRemark(record.getRecordTime() + "样品已由" + userName + "完成最终处理。");
        labSampleRecordMapper.insert(record);
    }

    /**
     * 样品出库 写入样品流转记录
     *
     * @param labEntrustSample
     * @param loginUser
     */
    @Override
    public void addOutRecords(LabEntrustSample labEntrustSample, LoginUser loginUser) {
        String userName = loginUser.getRealname();

        LabSampleRecord record = new LabSampleRecord();
        record.setSendPerson(userName);
        record.setRecordRemark(DateUtils.formatDateTime() + "由" + labEntrustSample.getOutPerson() + "将样品从仓库领用出来。");
        record.setSamType(labEntrustSample.getSamType());
        record.setSamId(labEntrustSample.getId());
        record.setSamName(labEntrustSample.getSamName());
        record.setSamNum(labEntrustSample.getSamNum());
        record.setRecordMark("出库");
        record.setReceivePerson(labEntrustSample.getOutPerson());
        record.setOperatePerson(userName);
        record.setRecordTime(new DateTime());
        record.setBusId(labEntrustSample.getEntrustId());
        String quality = labEntrustSample.getSamQuality();
        if (quality != null && !"".equals(quality)) {
            quality = quality.replace("g", "").replace("k", "").replace("m", "")
                    .replace("G", "").replace("K", "").replace("M", "").replace("L", "")
                    .replace("(", "").replace(")", "").replace("/", "");
            if (quality.matches("-?\\d+(\\.\\d+)?")) {
                record.setSamQuantity(Double.valueOf(quality));
            } else {
                record.setSamQuantity(0.00);
            }
        } else {
            record.setSamQuantity(0.00);
        }

        record.setSynFlag("0");
        labSampleRecordMapper.insert(record);
    }


}
